Skip to content

IconDelegate

Base classes

Name Children Inherits
StyledItemDelegate
prettyqt.widgets.styleditemdelegate

⋔ Inheritance diagram

graph TD
  6271601["itemdelegates.IconDelegate"]
  4109708588["widgets.StyledItemDelegate"]
  3153451512["widgets.AbstractItemDelegateMixin"]
  3227531497["core.ObjectMixin"]
  2751680898["builtins.object"]
  3599391368["QtWidgets.QStyledItemDelegate"]
  475576970["QtWidgets.QAbstractItemDelegate"]
  3984060476["QtCore.QObject"]
  37984305["Shiboken.Object"]
  4109708588 --> 6271601
  3153451512 --> 4109708588
  3227531497 --> 3153451512
  2751680898 --> 3227531497
  2751680898 --> 3153451512
  3599391368 --> 4109708588
  475576970 --> 3599391368
  3984060476 --> 475576970
  37984305 --> 3984060476
  2751680898 --> 37984305

🛈 DocStrings

Bases: StyledItemDelegate

Delegate to paint QIcons, QPixmaps, QColors and QImages.

Source code in prettyqt/itemdelegates/icondelegate.py
class IconDelegate(widgets.StyledItemDelegate):
    """Delegate to paint QIcons, QPixmaps, QColors and QImages."""

    ID = "icon"

    def __init__(self, role: constants.ItemDataRole = constants.USER_ROLE, **kwargs):
        self._role = role
        self.margin = 10
        super().__init__(**kwargs)

    def paint(
        self,
        painter: gui.QPainter,
        option: widgets.QStyleOptionViewItem,
        index: core.ModelIndex,
    ):
        """Override to paint an icon based on given Pixmap / Color / Icon.

        Pixmap / Color / Icon must be set to '_role'

        Args:
            painter (gui.QPainter): painter to paint the icon
            option (widgets.QStyleOptionViewItem): state of the item to be displayed
            index (core.ModelIndex): index which gets decorated
        """
        super().paint(painter, option, index)
        value = index.data(self._role)
        if not value:
            return
        mode = gui.Icon.Mode.Normal

        if not option.state & widgets.Style.StateFlag.State_Enabled:
            mode = gui.Icon.Mode.Disabled
        elif option.state & widgets.Style.StateFlag.State_Selected:
            mode = gui.Icon.Mode.Selected
        match value:
            case gui.QPixmap():
                icon = gui.QIcon(value)
                option.decorationSize = value.size() / value.devicePixelRatio()

            case gui.QColor():
                pixmap = gui.QPixmap(option.decorationSize)
                pixmap.fill(value)
                icon = gui.QIcon(pixmap)

            case gui.QImage():
                icon = gui.QIcon(gui.QPixmap.fromImage(value))
                option.decorationSize = value.size() / value.devicePixelRatio()

            case gui.QIcon():
                icon = value
                is_on = option.state & widgets.Style.StateFlag.State_Open
                state = gui.Icon.State.On if is_on else gui.Icon.State.Off
                actual_size = icon.actualSize(option.decorationSize, mode, state)
                option.decorationSize = option.decorationSize.boundedTo(actual_size)
            case _:
                raise ValueError(value)
        r = core.Rect(core.Point(), cast("core.QSize", option.decorationSize))
        r.moveCenter(option.rect.center())
        r.setRight(option.rect.right() - self.margin)
        state = (
            gui.Icon.State.On
            if option.state & widgets.Style.StateFlag.State_Open
            else gui.Icon.State.Off
        )
        alignment = constants.ALIGN_RIGHT | constants.ALIGN_V_CENTER
        icon.paint(painter, r, alignment, mode, state)

paint(painter: gui.QPainter, option: widgets.QStyleOptionViewItem, index: core.ModelIndex)

Override to paint an icon based on given Pixmap / Color / Icon.

Pixmap / Color / Icon must be set to '_role'

Parameters:

Name Type Description Default
painter QPainter

painter to paint the icon

required
option QStyleOptionViewItem

state of the item to be displayed

required
index ModelIndex

index which gets decorated

required
Source code in prettyqt/itemdelegates/icondelegate.py
def paint(
    self,
    painter: gui.QPainter,
    option: widgets.QStyleOptionViewItem,
    index: core.ModelIndex,
):
    """Override to paint an icon based on given Pixmap / Color / Icon.

    Pixmap / Color / Icon must be set to '_role'

    Args:
        painter (gui.QPainter): painter to paint the icon
        option (widgets.QStyleOptionViewItem): state of the item to be displayed
        index (core.ModelIndex): index which gets decorated
    """
    super().paint(painter, option, index)
    value = index.data(self._role)
    if not value:
        return
    mode = gui.Icon.Mode.Normal

    if not option.state & widgets.Style.StateFlag.State_Enabled:
        mode = gui.Icon.Mode.Disabled
    elif option.state & widgets.Style.StateFlag.State_Selected:
        mode = gui.Icon.Mode.Selected
    match value:
        case gui.QPixmap():
            icon = gui.QIcon(value)
            option.decorationSize = value.size() / value.devicePixelRatio()

        case gui.QColor():
            pixmap = gui.QPixmap(option.decorationSize)
            pixmap.fill(value)
            icon = gui.QIcon(pixmap)

        case gui.QImage():
            icon = gui.QIcon(gui.QPixmap.fromImage(value))
            option.decorationSize = value.size() / value.devicePixelRatio()

        case gui.QIcon():
            icon = value
            is_on = option.state & widgets.Style.StateFlag.State_Open
            state = gui.Icon.State.On if is_on else gui.Icon.State.Off
            actual_size = icon.actualSize(option.decorationSize, mode, state)
            option.decorationSize = option.decorationSize.boundedTo(actual_size)
        case _:
            raise ValueError(value)
    r = core.Rect(core.Point(), cast("core.QSize", option.decorationSize))
    r.moveCenter(option.rect.center())
    r.setRight(option.rect.right() - self.margin)
    state = (
        gui.Icon.State.On
        if option.state & widgets.Style.StateFlag.State_Open
        else gui.Icon.State.Off
    )
    alignment = constants.ALIGN_RIGHT | constants.ALIGN_V_CENTER
    icon.paint(painter, r, alignment, mode, state)

:fa-brands-github: Show source on GitHub